<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="记录名称" prop="recordName">
        <el-input
          v-model="queryParams.recordName"
          placeholder="请输入记录名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['baseinfo:waterRecord:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['baseinfo:waterRecord:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['baseinfo:waterRecord:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="importTemplate"
        >下载导入模板</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="waterRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="记录名称" align="center" prop="recordName" width="260" />
      <el-table-column label="极大值阈值" align="center" prop="threshold" />
      <el-table-column label="极小值阈值" align="center" prop="thresholdmin" />
<!--      <el-table-column label="水流温度" align="center" prop="waterTemperature" />-->
      <el-table-column label="计算状态" align="center" prop="calcStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.inversion_status" :value="scope.row.calcStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createdBy" />
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.calcStatus == '2'"
            size="mini"
            type="text"
            icon="el-icon-zoom-in"
            @click="handleLook(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['baseinfo:waterRecord:edit']"
          >计算</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['baseinfo:waterRecord:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改水脉分析记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="选择文件" v-if="!form.filePath">
          <el-row>
            <el-col :span="24" class="upload">
              <el-upload
                ref="upload"
                :limit="1"
                accept=".xlsx, .xls"
                :headers="upload.headers"
                :action="upload.url + '?updateSupport=' + upload.updateSupport"
                :disabled="upload.isUploading"
                :auto-upload="true"
                :show-file-list="true"
                :on-success="handleFileSuccess"
                :on-remove="handleFileRemove"
                :on-progress="handleFileProgress"
                drag
              >
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                <div class="el-upload__tip text-center" slot="tip">
                </div>
              </el-upload>
            </el-col>
          </el-row>
        </el-form-item>
        <div v-if="activeer" style="text-align: center">
              <img style="height: 40px" src="../../../assets/img/loading.gif"><div style="display: inline-block;height: 40px;line-height: 40px;vertical-align: top;margin-top: 5px">正在生成等值线图,请稍后...</div>
         </div>
        <el-form-item v-if="form.filePath" label="等值线图" >
          <img style="width: 100%" :src="form.filePath">
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="记录名称" prop="recordName">
              <el-input v-model="form.recordName" placeholder="请输入记录名称" />
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">-->
<!--            <el-form-item label="水流温度" prop="waterTemperature">-->
<!--              <el-input v-model="form.waterTemperature" placeholder="请输入水流温度" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="8">
            <el-form-item label="极大阈值" prop="threshold" >
              <el-input v-model="form.threshold" placeholder="请输入阈值" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="极小阈值" prop="thresholdmin">
              <el-input v-model="form.thresholdmin" placeholder="请输入阈值" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div v-if="activeer1" style="text-align: center">
              <img style="height: 40px" src="../../../assets/img/loading.gif"><div style="display: inline-block;height: 40px;line-height: 40px;vertical-align: top;margin-top: 5px">正在拼命计算中,请稍后...</div>
         </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">计 算</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="openResultTitle" :visible.sync="openResult" @close="cancelResult" class="rizhi" width="800px"   >
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="分析参数" name="first">
          <el-form ref="form" :model="form" label-width="80px">
            <el-row>
              <el-col :span="10">
                <el-form-item label="记录名称" prop="recordName">
                  {{form.recordName}}
                </el-form-item>
              </el-col>
              <el-col :span="7">
                <el-form-item label="极大阈值" prop="threshold">
                  {{form.threshold}}
                </el-form-item>
              </el-col>
              <el-col :span="7">
                <el-form-item label="极小阈值" prop="threshold">
                  {{form.thresholdmin}}
                </el-form-item>
              </el-col>
<!--              <el-col :span="7">-->
<!--                <el-form-item label="水流温度" prop="waterTemperature">-->
<!--                  {{form.waterTemperature}}-->
<!--                </el-form-item>-->
<!--              </el-col>-->
            </el-row>
            <el-form-item v-if="form.filePath" label="等值线图" >
              <img style="width: 100%" :src="form.filePath">
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="分析结果" name="second">
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                @click="handleExport"
              >导出结果</el-button>
            </el-col>
          </el-row>
          <el-table  :data="resultList">
            <el-table-column label="极值点温度" align="center" prop="t0" />
            <el-table-column label="tb值" align="center" prop="tb" />
            <el-table-column label="Xi值" align="center" prop="xi" />
            <el-table-column label="a值" align="center" prop="a" />
            <el-table-column label="深度h值" align="center" prop="h" />
            <el-table-column label="半径r值" align="center" prop="r" />
          </el-table>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="rizhi-dialog-footer">
        <el-button @click="cancelResult">关闭</el-button>
      </div>
    </el-dialog>
  </div>



</template>

<script>
import { listWaterRecord, getWaterRecord, delWaterRecord,updateWaterRecord } from "@/api/baseinfo/waterRecord";
//别忘了 开发文档没有会报错
import { getToken } from "@/utils/auth";
import {getWaterResultList} from "@/api/baseinfo/waterResult";
export default {
  name: "WaterRecord",
  dicts: ['inversion_status'],
  data() {
    return {
      activeer1:false,
      activeer:0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 水脉分析记录表格数据
      waterRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      activeName:'first',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        recordName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        recordName: [
          { required: true, message: "分析记录名称不能为空", trigger: "blur" },

        ],
        threshold: [
          { required: true, message: "极大值阈值不能为空", trigger: "blur" }
        ],
        thresholdmin:[
          { required: true, message: "极小值阈值不能为空", trigger: "blur" }
        ],
        waterTemperature: [
          { required: true, message: "水流温度不能为空", trigger: "blur" },
        ]
      },
      // 用户导入参数
      upload: {
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/baseinfo/waterRecord/importData",
        // 是否更新已经存在的用户数据
        updateSupport: 0,
      },
      openResult:false,
      openResultTitle:"",
      resultList:[]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询水脉分析记录列表 */
    getList() {
      this.loading = true;
      listWaterRecord(this.queryParams).then(response => {
        this.waterRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();


    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        recordName: null,
        threshold: null,
        waterTemperature: null,
        calcStatus: "0",
        createdBy: null,
        createdTime: null,
        updatedBy: null,
        updatedTime: null,
        filePath:null,
        thresholdmin:null
      };
      this.activeer1=false;
      this.activeer= false;
      this.activeName = 'first',
      this.resetForm("form");
    },
    handleFileProgress(){
      this.activeer=true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      console.log("----文件上传成功钩子-------")
      console.log(response)
      if(response.code == 200){
        this.form = response.data
        this.getList();
      }else {
        this.$modal.msgError("文件导入失败,请检查文件数据格式是否正确!")
      }
      this.$refs.upload.clearFiles();
      this.activeer=false

    },
    // 取消上上传的钩子
    handleFileRemove(){
      this.activeer=false

    },
    // 取消附件按钮
    cancelResult() {
      this.openResult= false;
      this.reset();
      this.resultList=[];
      this.activeer=false

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加水脉分析记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWaterRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "水脉分析计算";
      });
    },
    handleLook(row){
      this.openResult = true;
      this.openResultTitle = "计算结果详情";
      this.handleDetail(row.id)
    },
    handleDetail(id){
      this.reset();
      getWaterRecord(id).then(response => {
        this.form = response.data;
      });
    },
    handleShow(id){
      this.resultList =[];
      getWaterResultList(id).then(response=>{
        this.resultList=response.data;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.activeer1=true;
      if(this.form.id == null){
        this.$modal.msgWarning("请先导入数据再进行计算！")
        return;
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWaterRecord(this.form).then(response => {
              this.$modal.msgSuccess("计算完成！");
              this.activeer1=false;
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除水脉分析记录编号为"' + ids + '"的数据项？').then(function() {
        return delWaterRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 下载模板操作 */
    importTemplate() {
      this.download('baseinfo/waterRecord/importTemplate', {
      }, `温度数据_${new Date().getTime()}.xlsx`)
    },
    handleClick(tab, event) {
      if(this.activeName == 'first'){
        this.handleDetail(this.form.id)
      }else if(this.activeName == 'second'){
        this.handleShow(this.form.id)
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('baseinfo/waterResult/export', {
        recordId:this.form.id
      }, this.form.recordName+`_result.xlsx`)
    }
  }
};
</script>
<style >
.upload .el-upload-dragger{
  width: 680px!important;
}
</style>
